﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;

public partial class OutSourcing : System.Web.UI.Page
{
    //creamos el dataset que contendra las tablas
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["iduser"] == null || Session["iduser"].ToString() == "0")
            {
                Response.Redirect("index.aspx");
            }
            else
            {
                lblses.Text = "Bienvenido " + (string)(Session["Ncom"]);
                //llenamos el dataset con los datos de la tablas de Outsourcing                                    
                llenarficha();
            }
        }
    }
  
    //-----------------------------------------------------------------------------------------------------
    void llenarficha()
    {
        try
        {
            String User = (string)(Session["iduser"]);
            DataSet dset = new DataSet();
            Usuarios.Usuario usu = new Usuarios.Usuario();
            dset = usu.estandarweb("Select * from Outsourcing where IdOutsourcing = " + User, "Outsourcing");

            if (dset.Tables["Outsourcing"].Rows.Count > 0)
            {

                lblsave.Visible = false;
                imgbtnEm.Visible = false;
                lblact.Visible = true;
                btnact.Visible = true;
                LinkButton1.Visible = true;
                txtOutnombre.Text = dset.Tables["Outsourcing"].Rows[0]["Nombre"].ToString();
                TxtOutNomCom.Text = dset.Tables["Outsourcing"].Rows[0]["NombreComercial"].ToString();
                TxtOutReg.Text = dset.Tables["Outsourcing"].Rows[0]["NoRegistro"].ToString();
                TxtOutNumTri.Text = dset.Tables["Outsourcing"].Rows[0]["CodigoTributario"].ToString();
                TxtOutGiro.Text = dset.Tables["Outsourcing"].Rows[0]["Giro"].ToString();
                OutDirecion.Text = dset.Tables["Outsourcing"].Rows[0]["Direccion"].ToString();
                TxtOutTel.Text = dset.Tables["Outsourcing"].Rows[0]["Telefono"].ToString();
                TxtOutFax.Text = dset.Tables["Outsourcing"].Rows[0]["Fax"].ToString();
                TxtOutContacto.Text = dset.Tables["Outsourcing"].Rows[0]["Contacto"].ToString();
                TxtOutPagWeb.Text = dset.Tables["Outsourcing"].Rows[0]["WWW"].ToString();
                txtOutDes.Text = dset.Tables["Outsourcing"].Rows[0]["Descripcion"].ToString();
                TxtOutcorreoElectronico.Text = dset.Tables["Outsourcing"].Rows[0]["Email"].ToString();

            }
        }
        catch (Exception ex)
        {


            EventLog LogErr = new EventLog();
            if (!EventLog.SourceExists("WebContrataci"))
            {
                EventLog.CreateEventSource("WebContrataci", "Application");
                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString(), EventLogEntryType.Information, 1);
            }
            else
            {

                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString());
            }
        }
    }
    //-----------------------------------------------------------------------------------------------------

    protected void LinkButton8_Click(object sender, EventArgs e)
    {
        Response.RedirectPermanent("index.aspx");
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        Response.RedirectPermanent("index.aspx");
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        Response.Redirect("OutSourcing.aspx");
    }
    protected void LinkButton7_Click(object sender, EventArgs e)
    {
        Response.Redirect("Reportes.aspx");
    } 
    protected void LinkButton9_Click(object sender, EventArgs e)
    {
        Response.Redirect("OutSourcing.aspx");
    }

    protected void LinkButton12_Click(object sender, EventArgs e)
    {
        Response.Redirect("Reportes.aspx");
    }
    //-----------------------------------------------------------
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        try
        {
            String User = (string)(Session["iduser"]);
            DataSet dset = new DataSet();
            Usuarios.Usuario usu = new Usuarios.Usuario();
            dset = usu.estandarweb("Select * from Outsourcing where IdOutsourcing = " + User, "Outsourcing");

            if (dset.Tables["Outsourcing"].Rows.Count > 0)
            {
                dset.Tables["Outsourcing"].Rows[0].Delete();

                Outs.Outsourcing cl = new Outs.Outsourcing();
                cl.DatoOutSourcing(dset);
                dset.AcceptChanges();
                Response.Redirect("Outsourcing.aspx");
            }
        }
        catch (Exception ex)
        {


            EventLog LogErr = new EventLog();
            if (!EventLog.SourceExists("WebContrataci"))
            {
                EventLog.CreateEventSource("WebContrataci", "Application");
                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString(), EventLogEntryType.Information, 1);
            }
            else
            {

                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString());
            }
        }
    }
    //-------------------------------------------------------------------
    protected void btnact_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            String User = (string)(Session["iduser"]);
            DataSet dset = new DataSet();
            Usuarios.Usuario usu = new Usuarios.Usuario();
            dset = usu.estandarweb("Select * from Outsourcing where IdOutsourcing = " + User, "Outsourcing");

            if (dset.Tables["Outsourcing"].Rows.Count > 0)
            {
                //ingrasamos los datos de la fila
                dset.Tables["Outsourcing"].Rows[0].BeginEdit();
                dset.Tables["Outsourcing"].Rows[0]["IdOutsourcing"] = User;
                dset.Tables["Outsourcing"].Rows[0]["Nombre"] = txtOutnombre.Text;
                dset.Tables["Outsourcing"].Rows[0]["NombreComercial"] = TxtOutNomCom.Text;
                dset.Tables["Outsourcing"].Rows[0]["NoRegistro"] = TxtOutReg.Text;
                dset.Tables["Outsourcing"].Rows[0]["CodigoTributario"] = TxtOutNumTri.Text;
                dset.Tables["Outsourcing"].Rows[0]["Giro"] = TxtOutGiro.Text;
                dset.Tables["Outsourcing"].Rows[0]["Direccion"] = OutDirecion.Text;
                dset.Tables["Outsourcing"].Rows[0]["Telefono"] = TxtOutTel.Text;
                dset.Tables["Outsourcing"].Rows[0]["Fax"] = TxtOutFax.Text;
                dset.Tables["Outsourcing"].Rows[0]["Contacto"] = TxtOutContacto.Text;
                dset.Tables["Outsourcing"].Rows[0]["WWW"] = TxtOutPagWeb.Text;
                dset.Tables["Outsourcing"].Rows[0]["Email"] = TxtOutcorreoElectronico.Text;
                dset.Tables["Outsourcing"].Rows[0]["Descripcion"] = txtOutDes.Text;

                dset.Tables["Outsourcing"].Rows[0].EndEdit();


                Outs.Outsourcing cl = new Outs.Outsourcing();
                cl.DatoOutSourcing(dset);
                dset.AcceptChanges();
                lblsave.Text = "Datos Guardados";


            }
        }
        catch (Exception ex)
        {


            EventLog LogErr = new EventLog();
            if (!EventLog.SourceExists("WebContrataci"))
            {
                EventLog.CreateEventSource("WebContrataci", "Application");
                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString(), EventLogEntryType.Information, 1);
            }
            else
            {

                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString());
            }
        }
        
    }
    protected void imgbtnEm_Click(object sender, ImageClickEventArgs e)
    {

        try
        {
            //creamos el objeto tabla para llenar una nueva fila
            DataSet ds = new DataSet();
            //llenamos el dataset con los datos de la tablas de Outsourcing
            Outs.Outsourcing O = new Outs.Outsourcing();
            ds = O.ConsultaOutSourcing();
            String User = (string)(Session["iduser"]);
            DataTable TOut = ds.Tables["Outsourcing"];
            DataRow row = TOut.NewRow();
            //ingrasamos los datos de la fila
            row["IdOutsourcing"] = User;
            row["Nombre"] = txtOutnombre.Text;
            row["NombreComercial"] = TxtOutNomCom.Text;
            row["NoRegistro"] = TxtOutReg.Text;
            row["CodigoTributario"] = TxtOutNumTri.Text;
            row["Giro"] = TxtOutGiro.Text;
            row["Direccion"] = OutDirecion.Text;
            row["Telefono"] = TxtOutTel.Text;
            row["Fax"] = TxtOutFax.Text;
            row["Contacto"] = TxtOutContacto.Text;
            row["WWW"] = TxtOutPagWeb.Text;
            row["Descripcion"] = txtOutDes.Text;
            row["Email"] = TxtOutcorreoElectronico.Text;


            ds.Tables["Outsourcing"].Rows.Add(row);

            Outs.Outsourcing cl = new Outs.Outsourcing();
            cl.DatoOutSourcing(ds);
            ds.AcceptChanges();
        }
        catch (Exception ex)
        {


            EventLog LogErr = new EventLog();
            if (!EventLog.SourceExists("WebContrataci"))
            {
                EventLog.CreateEventSource("WebContrataci", "Application");
                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString(), EventLogEntryType.Information, 1);
            }
            else
            {

                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString());
            }
        }
    }
}